package com.cctv.video.base.adapter;

import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.RecycleItemProvider;
import ohos.app.Context;

import java.util.List;

/**
 * adapter基类
 *
 * @param <T>
 */
public abstract class RecycleItemProviderAdapter<T> extends RecycleItemProvider {
    protected Context  mContext;
    protected List<T> mDatas;
//    protected LayoutScatter mInflater;
    private int layoutId;
    private int displayCount;

    public RecycleItemProviderAdapter(Context  context, List<T> datas) {
        this.mContext = context;
        this.mDatas = datas;
//        mInflater = LayoutScatter.getInstance(mContext);
    }

    public RecycleItemProviderAdapter(Context context, List<T> datas, int layoutId) {
        this.mContext = context;
        this.mDatas = datas;
        this.layoutId = layoutId;
//        mInflater = LayoutScatter.getInstance(mContext);
    }

    @Override
    public int getCount() {
        if (displayCount != 0) {
            return this.displayCount;
        }
        return mDatas.size();
    }

    public void setDisplayCount(int displayCount) {
        this.displayCount = displayCount;
    }

    @Override
    public T getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component convertView, ComponentContainer parent) {

        ItemViewHolder holder = ItemViewHolder.get(mContext, convertView, parent,
                layoutId, position);
        convert(holder, getItem(position));
        return holder.getConvertView();
    }

    public List<T> getDatas() {
        return mDatas;
    }

    public void setDatas(List<T> datas) {
        mDatas = datas;
    }

    public abstract void convert(ItemViewHolder holder, T t);

}
